package com.atguigu.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entites.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.xml.transform.Result;
import java.util.List;

/**
 * @author BLADE
 * @description:
 * @data 2024/4/1511:19
 */
@RestController
public class OrderController {
//consul服务注册中心上的微服务名称
    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO){
        System.out.println("feign调用");
        ResultData resultData= payFeignApi.addPay(payDTO);
        return resultData;
    }
    @GetMapping("/feign/pay/getById/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
       System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
       ResultData resultData=null;
       try {
           System.out.println("调用开始--------"+ DateUtil.now());
            resultData= payFeignApi.getPayInfo(id);
       }catch (Exception e){
           e.printStackTrace();
           return ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
       }finally {
           System.out.println("调用结束--------"+ DateUtil.now());
       }
       return resultData;
    }

    /**
     * openfeign天然支持负载均衡演示
     * @return
     */
    @GetMapping("/fegin/payb/mylb")
    public String mylb(){
        return payFeignApi.mylb();
    }
}
